//=============创建发货通知单.js


function setPresonAddr(evt) {


    h3ui.models.t_deliver_notice.item.receiver = evt.data.row.receiver_nameb;
    h3ui.models.t_deliver_notice.item.phone = evt.data.row.mobile;
    h3ui.models.t_deliver_notice.item.postal = evt.data.row.post;
    h3ui.models.t_deliver_notice.item.address = evt.data.row.address;


    h3ui.widgets[evt.node.parentId].props.visible = false;

}


function initstart() {
    console.log(444);

    if (h3ui.$router.currentRoute.query.mode == "create") {
        var dataSource = h3ui.models.t_deliver_notice;

        //页面加载后获取创建发货通知单数据填充表单
        dataSource.subscribe('inited', function () {
            var ids = h3ui.$router.currentRoute.query.ids;
            if (ids) {
                ids = ids.split(",");
                h3ui.services.getDeliverNotice({ids}).then(res => {
                    console.log(res);
                    res.data.c52m8y5i3nno.forEach((a, index) => {
                        a.id = index + 1;
                    });

                    const item = JSON.parse(JSON.stringify(res.data));
                    // for(const key in item) {
                    //     dataSource.item[key] = item[key];
                    // };
                    dataSource.item = item;

                    if (dataSource.item.vendor_id && dataSource.item.vendor_id.length > 0) {
                        dataSource.item.vendor_name = dataSource.item.vendor_id[0].name;
                    }

                    if (dataSource.item.customer_id && dataSource.item.customer_id.length > 0) {
                        dataSource.item.customer_name = dataSource.item.customer_id[0].name;
                    }

                    if (dataSource.item.member_id && dataSource.item.member_id.length > 0) {
                        dataSource.item.member_name = member_id.name;
                    }


                    //子表列可编辑控制
                    listColsEdit("FormSheet10", ["min_num"]);

                    // 清空监听：
                    dataSource.subscribe('inited', function () {
                    });
                });
            }
        });


    } else {
        if (h3ui.$router.currentRoute.query.mode == "detail") {
            //查看
            var bizObjectId = h3ui.$router.currentRoute.query.clickcloumn;
            h3ui.models.t_deliver_notice.loadBO({bizObjectId}).then(res => {
                console.log(res);
                h3ui.models.t_deliver_notice.item = res;
            });
            //设置表单不可编辑
            h3ui.widgets.FormModel6.props.editMode = false;
        } else {
            h3ui.widgets.FormModel6.props.editMode = false;
        }

        ["Buttons53", "Buttons77", "Buttons78", "Buttons52_Modal1"].forEach(a => h3ui.widgets[a].props.visible.display = "none");
    }
}


/**
 *  子表列表可编辑控制
 * @param {*} listId
 * @param {*} editCodeArr
 */
function listColsEdit(listId, editCodeArr) {
    var list = h3ui.widgets[listId];
    var colsStatus = list.props.colsStatus;
    Object.keys(colsStatus).forEach(str => {
        colsStatus[str].writable = editCodeArr.includes(str);
    })

    list.props.colsStatus = Object.assign({}, colsStatus);

}


/**
 * 打开对话框,按钮的id 设置为 xxx_Modalx 如: Buttons_Modal1
 */
function openModal(evt) {
    debugger;
    var modalId = evt.node.id.split("_")[1];
    h3ui.widgets[modalId].props.visible = true;
}

/**
 * 关闭对话框
 */
function closeModal(evt) {
    var modalId = evt.node.id.split("_")[1];
    h3ui.widgets[modalId].props.visible = false;
}


//按钮事件, 保存 mySave,提交 mySubmit,返回myBack
function myButtonEvt(evt) {
    console.log(evt);

    var code = evt.data.code;
    var dataSource = h3ui.models.t_deliver_notice;

    if (code == "myBack") {
        setTimeout(window.history.back(), 2);
    } else if (code == "mySubmit" || code == "mySave") {

        var item = h3ui.models.t_deliver_notice.item;

        if (item.member_id && item.member_id.length > 0) {
            if (item.member_id instanceof Array) {
                item.member_name = item.member_id[0].name;
            } else {
                item.member_name = item.member_id.name;
            }
        }

        if (item.vendor_id && item.vendor_id.length > 0) {
            if (item.vendor_id instanceof Array) {
                item.vendor_name = item.vendor_id[0].name;
            } else {
                item.vendor_name = item.vendor_id.name;
            }
        }
        var approvalStatus = code == "mySubmit" ? "2" : "1";
        dataSource.item.approvalStatus = approvalStatus;
        var isNew = !dataSource.item.id;
        dataSource.item.c52m8y5i3nno.forEach(a => {
            a.$new = isNew;
            a.approvalStatus = approvalStatus
        });

        dataSource.submitBO().then(res => {
            console.log(res);

            // if(code =="mySave"){
            //     h3ui.$message.info("保存成功");

            //     if(!dataSource.item.id){
            //         dataSource.item.id = res.bizObjectId;
            //         if(res.data.createdTime){
            //             dataSource.item.createdTime = res.data.createdTime;
            //         }                    
            //     }

            // }else{
            //     h3ui.$message.info("提交成功");
            //     setTimeout(window.history.back(),2);
            // }


            toModePage("delivergoods", {}, "ordercenter");


        })

    }
}

/**
 *  跳转表单页面并url携带参数
 *  pageCode // 页面code *
 *  param    // 参数
 *  appCode  // 应用code
 */
function toModePage(pageCode, param, appCode) {
    // 获取页面url地址
    let url = window.location.href.substring(0, window.location.href.indexOf('?'));

    if (appCode) {
        url = url.replaceAll(h3ui.appInfo.appCode, appCode);
    }

    var paramstr = Object.keys(param).map(a => {
        return "&".concat(a).concat("=").concat(param[a])
    }).join("");

    // 跳转创建页面
    window.location.href = url + '?pageCode=' + pageCode + paramstr;
}
